package com.mikhailenko.transportMVC.utils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

@Component
public class TableUtil {
    
    public List<List<String>> fill(List<List<String>> list){
        String [][] mas = new String[list.size()][list.size()];
        List<List<String>> result = new ArrayList<List<String>>();
        
        for (int i = 0; i < list.size(); i++){
            List<String> row = new ArrayList<String>();
            for (int j = 0; j < list.get(i).size(); j++){
                if (i <= j){
                    mas[i][j] = list.get(i).get(j);
                } else {
                    mas[i][j] = getReverseNumbers(list.get(j).get(i));
                }
                row.add(mas[i][j]);
            }
            result.add(row);
        }
        
        return result;
    }
    
    private String getReverseNumbers(String triangleNumber){
        String[] numbers = triangleNumber.split(",");
        StringBuilder result = new StringBuilder();
        String[] numbersReverse = new String[triangleNumber.length()];
        for (int i = 0; i < numbers.length; i++){
            numbersReverse[i] = "1/"+numbers[numbers.length - 1 - i];
            result.append(numbersReverse[i]);
            result.append(", ");
        }
        result.deleteCharAt(result.lastIndexOf(","));
        return result.toString();
    }
    
}
